package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行查询语句
 */
public class JDBCDemo4 {
    public static void main(String[] args) {
        /*
            查看老师的相关信息
            id,name,title,salary
         */
        try (
                Connection connection = DBUtil.getConnection();
        ){
            Statement statement = connection.createStatement();
            String sql = "SELECT id,name,title,salary " +
                         "FROM teacher";
            /*
                Statement提供了专门用于执行DQL语句的方法:
                ResultSet executeQuery(String sql)
                返回的ResultSet代表查询的结果集
             */
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String title = rs.getString("title");
                int salary = rs.getInt("salary");
                System.out.println(id + "," + name + "," + title + "," + salary);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}





